{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#encoding:utf-8\n",
    "# 文件操作\n",
    "from operator import *\n",
    "# 正则表达式\n",
    "import re\n",
    "# url解码用的\n",
    "import urllib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 打开文件\n",
    "f = open('src/ff_11281116.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 链接们\n",
    "links = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 正则\n",
    "linkPattern = re.compile(r'(href=\".*?\\\")')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# # 取到连接\n",
    "for line in f.readlines():\n",
    "    links = linkPattern.findall(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 参数值们\n",
    "paras = []\n",
    "# 用正则从链接中匹配参数值\n",
    "paraPattern = re.compile(r'(\\=.*?&amp;)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 链接修剪并匹配\n",
    "for link in links:\n",
    "    # 修剪\n",
    "    link = link[6:-1]\n",
    "    # 匹配\n",
    "    paraList = paraPattern.findall(link)\n",
    "    for para in paraList:\n",
    "        paras.append(unicode(para[1:-5],'utf-8'))\n",
    "            "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": false
   },
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "13084236\n",
      "1\n",
      "2016-11-28 11:15:38\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "firefox-50.0-complete\n",
      "osx\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "basicSearch\n",
      "navclient-auto-ffox\n",
      "45.0.1\n",
      "basicSearch\n",
      "basicSearch\n",
      "/portal/portal\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "http://i.hdu.edu.cn/dcp/dcp/sso/ssozadl.jsp\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "firefox-50.0-complete\n",
      "osx\n",
      "/portal/portal\n",
      "personalHomePage\n",
      "MjAxMzA4MTIyNzcz\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "/portal/portal\n",
      "activityHomePage\n",
      "activityHomePage\n",
      "null\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "/portal/portal\n",
      "shareHomePage\n",
      "null\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "dcp/apps/sso/jsp/ssoDcpSelf\n",
      "/portal/portal\n",
      "collectionHomePage\n",
      "null\n"
     ]
    }
   ],
   "source": [
    "# 写入文件\n",
    "output = open('src/ff_11281116.txt','w+')\n",
    "# 开始写入\n",
    "for para in paras:\n",
    "    output.write(urllib.unquote(line))\n",
    "output.close()\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
